package com.hs.srm.base.common.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

import com.hs.srm.base.common.base.CloneableEntity;

import sunnykid.Sunnykid;
import sunnykid.util.ClassUtils;
import sunnykid.util.UtilException;

/**
 * 实体类工具类
 * <p>標題：</p>
 * <p>描述：</p>
 * <p>版權：版權所有 (c) 2019</p>
 * <p>組織：厚声集团</p>
 *
 * @author 鐘曉籟
 * @version V1.0.0
 * @date 2019年1月29日 下午4:34:45
 * @since V1.0.0
 */
public class EntityUtils {

	/**
	 * 过滤太长的字符串
	 * @param t
	 * @param subLength
	 * @param exceptFields
	 * @return
	 */
	public static <T> T filterLongText(T t, int subLength, String... exceptFields) {
		T cloneT = Sunnykid.clone(t);
		List<String> exceptFieldList = Arrays.asList(exceptFields);
		if(cloneT == null) {
			return cloneT;
		}
		Set<Field> fieldSet = ClassUtils.getAllFields(cloneT.getClass());
		for(Field field : fieldSet) {
			if(field.getType() == String.class && !exceptFieldList.contains(field.getName())) {
				try {
					Object val = ClassUtils.getFieldValue(cloneT, field);
					if(val != null && val.toString().length() > subLength) {
						String vn = val.toString().substring(0, subLength) + "...";
						ClassUtils.setFieldValue(cloneT, field, vn);
					}
				} catch(UtilException ex) {
					throw new RuntimeException("处理实体中的长字符串失败", ex);
				}

			}
		}
		return cloneT;
	}

	/**
	 * 过滤太长的字符串
	 * @param list
	 * @param subLength
	 * @param exceptFields
	 * @return
	 */
	public static <T> List<T> filterLongText(List<T> list, int subLength, String... exceptFields) {
		if(list == null) {
			return list;
		}
		List<T> nList = new ArrayList<T>();
		for(T t : list) {
			nList.add(filterLongText(t, subLength, exceptFields));
		}
		return nList;
	}

	/**
	 * 过滤太长的字符串
	 * @param t
	 * @param exceptFields
	 * @return
	 */
	public static <T> T filterLongText(T t, String... exceptFields) {
		return filterLongText(t, 10, exceptFields);
	}

	/**
	 * 过滤太长的字符串
	 * @param list
	 * @param exceptField
	 * @return
	 */
	public static <T> List<T> filterLongText(List<T> list, String... exceptFields) {
		return filterLongText(list, 10, exceptFields);
	}

	/**
	 * 单个实体中的数据字典和省市区关联转化成text
	 * @param list
	 * @return
	 */
	public static <T extends CloneableEntity<?>> T valToTxt(T t) {
		try {
			T nt = DictDataUtil.parseToDictText(t);
			nt = AddressDataUtil.parseToAddressText(nt);
			return nt;
		} catch(UtilException ex) {
			throw new RuntimeException("转换字典数据或省市区数据失败！", ex);
		}
	}

	/**
	 * 单个实体中的数据字典和省市区关联转化成code
	 * @param list
	 * @return
	 */
	public static <T extends CloneableEntity<?>> T txtToVal(T t) {
		try {
			T nt = DictDataUtil.parseToDictCode(t);
			nt = AddressDataUtil.parseToAddressCode(nt);
			return nt;
		} catch(UtilException ex) {
			throw new RuntimeException("转换字典数据或省市区数据失败！", ex);
		}
	}

	/**
	 * 实体列表中的数据字典和省市区关联转化成text
	 * @param list
	 * @return
	 */
	public static <T extends CloneableEntity<?>> List<T> valToTxt(List<T> list) {
		if(list == null) {
			return list;
		}
		List<T> nList = new ArrayList<T>();
		for(T t : list) {
			try {
				T nt = DictDataUtil.parseToDictText(t);
				nt = AddressDataUtil.parseToAddressText(nt);
				nList.add(nt);
			} catch(UtilException ex) {
				throw new RuntimeException("转换字典数据或省市区数据失败！", ex);
			}
		}
		return nList;
	}

	/**
	 * 实体列表中的数据字典和省市区关联转化成code
	 * @param list
	 * @return
	 */
	public static <T extends CloneableEntity<?>> List<T> txtToVal(List<T> list) {
		if(list == null) {
			return list;
		}
		List<T> nList = new ArrayList<T>();
		for(T t : list) {
			try {
				T nt = DictDataUtil.parseToDictCode(t);
				nt = AddressDataUtil.parseToAddressCode(nt);
				nList.add(nt);
			} catch(UtilException ex) {
				throw new RuntimeException("转换字典数据或省市区数据失败！", ex);
			}

		}
		return nList;
	}

}
